package com.he.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Step;
import com.he.pojo.StepExample;
import com.he.pojo.StepExample.Criteria;
import com.he.pojo.User;
import com.he.pojo.Workflow;
import com.he.pojo.WorkflowStepDetail;
import com.he.pojo.WorkflowStepDetailExample;
import com.he.service.step.StepServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.workflow.WorkflowServiceI;
import com.he.service.workflowStepDetail.WorkflowStepDetailServiceI;

@Controller
@RequestMapping("myflow")
public class MyflowController {

	@Autowired
	private WorkflowServiceI wfi;
	@Autowired
	private StepServiceI ssi;
	@Autowired
	private WorkflowStepDetailServiceI wfdi;
	@Autowired
	private UserServiceI usi;

	@RequestMapping("show") // 显示流程列表
	public ModelAndView show() {
		ModelAndView mv = new ModelAndView("runflow");
		Workflow workflow=new Workflow();
		workflow.setWithPage(0);
		List<Workflow> list = wfi.getWorkflows(null, workflow);
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("runstep") // 填写表单
	public ModelAndView runStep(Integer id) {
		ModelAndView mv = new ModelAndView("addflow");
		// 查询指定流程的步骤信息
		StepExample stepExample = new StepExample();
		Criteria createCriteria = stepExample.createCriteria();
		createCriteria.andWorkflowIdEqualTo(id);
		List<Step> list = ssi.getSteps(stepExample, new Step());
		List<String> sLis = new ArrayList<>();
		for (Step stp : list) {
			sLis.add("'" + stp.getStepName() + "'");
		}
		// step步骤列表
		mv.addObject("list", list);
		// step名称列表，用于显示进度，待完成。。。
		mv.addObject("slist", sLis);
		Integer n = ssi.findMaxNum(id);
		// step数，用于显示进度，待完成。。。
		mv.addObject("scounts", n);
		// 传入流程id
		mv.addObject("workflowId", id);
		return mv;
	}

	@RequestMapping("saveform") // 保存表单信息
	@ResponseBody
	public String saveForm(WorkflowStepDetail workflowStepDetail, HttpServletRequest request) {
		// System.out.println(workflowStepDetail.getDetailInfo()+"
		// "+workflowStepDetail.getWorkflowId()+"
		// "+workflowStepDetail.getDetailTitle());
		// 通过session获得登录人id,目前假定
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		Integer userId = user.getId();		
		workflowStepDetail.setDetailDate(new Date());
		workflowStepDetail.setUserId(userId);
		workflowStepDetail.setStepNum(2);
		workflowStepDetail.setIsVerify(new Byte((byte) 0));
		int r = wfdi.insert(workflowStepDetail);
		if (r > 0) {
			return Integer.toString(workflowStepDetail.getUserId());
		} else {
			return "0";
		}
	}

	@RequestMapping("myapply") // 我的申请
	public ModelAndView showmyApple(Integer id,WorkflowStepDetail detail) {
		ModelAndView mv = new ModelAndView("myApplication");
		// 获得用户的所有流程申请
		WorkflowStepDetailExample workflowStepDetailExample = new WorkflowStepDetailExample();
		com.he.pojo.WorkflowStepDetailExample.Criteria createCriteria = workflowStepDetailExample.createCriteria();
		createCriteria.andUserIdEqualTo(id);
		List<WorkflowStepDetail> list = wfdi.getWorkflowStepDetails(workflowStepDetailExample,
				new WorkflowStepDetail());
		// 获得其中流程的名称，步骤名称
		for (WorkflowStepDetail wfkd : list) {
			Integer workflowId = wfkd.getWorkflowId();
			int stepNum = wfkd.getStepNum();
			Workflow workflow = wfi.getWorkflowById(workflowId);
			wfkd.setWorkflow(workflow);
			StepExample stepExample = new StepExample();
			Criteria cc = stepExample.createCriteria();
			cc.andWorkflowIdEqualTo(workflowId);
			cc.andStepNumEqualTo(stepNum);
			List<Step> steplist = ssi.getSteps(stepExample, new Step());
			for (Step step : steplist) {
				wfkd.setStep(step);
			}
		}
		mv.addObject("list", list);
		/*为workflow总页数赋值,向前端传总页数*/
		PageInfo<WorkflowStepDetail> info=new PageInfo<>(list);
		//为workflow总页数赋值
		detail.setPages(info.getPages());
		//为workflow总记录数赋值
		detail.setTotal(info.getTotal());
		/*为workflow返回的controller链接赋值*/
		detail.setUrl("show.do");
		/*向前端传参数保存在workflow中*/
		mv.addObject("detail", detail);
		return mv;
	}

	@RequestMapping("myapproval") // 待我审核
	public ModelAndView myApproval(Integer id,WorkflowStepDetail detail) {
		ModelAndView mv = new ModelAndView("myApproval");
		// 根据用户id，查询需要该用户审核的步骤集合
		StepExample stepExample = new StepExample();
		Criteria createCriteria = stepExample.createCriteria();
		createCriteria.andUserIdEqualTo(id);
		List<Step> steplist = ssi.getSteps(stepExample, new Step());
		// 根据步骤集合，查找符合条件的详细记录
		List<WorkflowStepDetail> wlist = new ArrayList<>();
		for (Step step : steplist) {
			int workflowId = step.getWorkflowId();
			int stepNum = step.getStepNum();
			WorkflowStepDetailExample workflowStepDetailExample = new WorkflowStepDetailExample();
			workflowStepDetailExample.createCriteria().andWorkflowIdEqualTo(workflowId).andStepNumEqualTo(stepNum)
					.andIsVerifyEqualTo(new Byte((byte) 0));
			List<WorkflowStepDetail> wwlist = wfdi.getWorkflowStepDetails(workflowStepDetailExample,
					new WorkflowStepDetail());
			for (WorkflowStepDetail workflowStepDetail : wwlist) {
				if (workflowStepDetail.getIsVerify().equals(new Byte((byte) 0))) {
					wlist.add(workflowStepDetail);
				}
			}
		}
		// 获得详情中申请人信息,获得详情中流程信息
		for (WorkflowStepDetail workflowStepDetail : wlist) {
			Integer userId = workflowStepDetail.getUserId();
			Integer workflowId = workflowStepDetail.getWorkflowId();
			User user = usi.getUserById(userId);
			Workflow workflow = wfi.getWorkflowById(workflowId);
			workflowStepDetail.setWorkflow(workflow);
			workflowStepDetail.setUser(user);

		}
		mv.addObject("list", wlist);
		/*为workflow总页数赋值,向前端传总页数*/
		PageInfo<WorkflowStepDetail> info=new PageInfo<>(wlist);
		//为workflow总页数赋值
		detail.setPages(info.getPages());
		//为workflow总记录数赋值
		detail.setTotal(info.getTotal());
		/*为workflow返回的controller链接赋值*/
		detail.setUrl("show.do");
		/*向前端传参数保存在workflow中*/
		mv.addObject("detail", detail);
		return mv;
	}

	@RequestMapping("approvalstep")
	@ResponseBody
	public String approvalStep(Integer id, HttpServletRequest request, String detailRemark) {
		String flag=request.getParameter("flag");
		// id为详情id，flag为通过与否的标识
		// 通过session获得登录人id,目前假定
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		Integer userId = user.getId();
		// 根据id获得指定的详情记录
		WorkflowStepDetail workflowStepDetail = wfdi.getWorkflowStepDetailById(id);
		// 获得该详情所属流程的步骤的总数，以确定后续操作
		Integer counts = ssi.findMaxNum(workflowStepDetail.getWorkflowId());
		Integer stepNum = workflowStepDetail.getStepNum();
		if (flag.equals("1")) {
			if (counts == (stepNum + 1)) {
				workflowStepDetail.setIsVerify(new Byte((byte) 1));
				workflowStepDetail.setStepNum(counts);
			} else {
				workflowStepDetail.setStepNum(stepNum + 1);
			}
		} else {
			workflowStepDetail.setIsVerify((byte) 2);
			workflowStepDetail.setDetailRemark(detailRemark);
		}
		// 更新表格
		int r = wfdi.update(workflowStepDetail);
		if (r > 0) {
			return Integer.toString(userId);
		} else {
			return "0";
		}
	}
}
